﻿using hospital_registration_system.Entity;
using LinkSql;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hospital_registration_system.DAL
{
    public class AppointmentDao
    {
        MySqlDataAdapter mySqlDataAdapter;
        private string tableName = "appointment";
        private string patient_id = "patient_id";
        private string illness_id = "illness_id";
        private string department_id = "department_id";
        private string doctor_id = "doctor_id";
        private string clinic_time_id = "clinic_time_id";



        /// <summary>
        /// 插入，病人id，疾病id，科室id,医生id，就诊日期(datatime),就诊时间段
        /// 
        /// </summary>
        /// <param name="patient_id"></param>
        /// <param name="illness_id"></param>
        /// <param name="department_id"></param>
        /// <param name="doctor_id"></param>
        /// <param name="clinic_time"></param>
        /// <param name="see_time"></param>
        /// <returns></returns>
        public bool insert(int patient_id,int illness_id, int department_id,int doctor_id,int clinic_time_id)
        {
            Dictionary<string, Object> map = new Dictionary<string, Object>();
            map.Add(this.patient_id,patient_id);
            map.Add(this.illness_id, illness_id);
            map.Add(this.department_id, department_id);
            map.Add(this.doctor_id, doctor_id);
            map.Add(this.clinic_time_id, clinic_time_id);
            map.Add("CREATED_BY", "病人");
            map.Add("CREATED_TIME", new DateTime());
            map.Add("UPDATED_BY", "病人");
            map.Add("UPDATED_TIME", new DateTime());
            map.Add("state", 0);
           
            return MySQLUtils.insert(tableName, map);
        }

        /// <summary>
        /// 
        /// 通过id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool delete(int id)
        {
            return MySQLUtils.delete(tableName, id);
        }

        /// <summary>
        ///  按id查询
        /// </summary>
        public Appointment selectById(int id)
        {
            return GetAppointmentsByDataTable(MySQLUtils.selectById(tableName, id))[0];
        }


        /// <summary>
        /// 通过map查询Appointment对象,将查询条件封装成键值对即可
        /// </summary>
        /// <returns> List<Appointment></returns>
        public List<Appointment> selectByMap(Dictionary<string, Object> map)
        {
            return GetAppointmentsByDataTable(MySQLUtils.selectByMap(tableName, map));
        }

        /// <summary>
        /// 查询所有Appointment
        /// </summary>
        public List<Appointment> selectAll()
        {
            return GetAppointmentsByDataTable(MySQLUtils.selectAll(tableName));
        }

        /// <summary>
        /// 将dataTable转化为List<Appointment>
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public List<Appointment> GetAppointmentsByDataTable(DataTable dataTable)
        {
            List<Appointment> list = new List<Appointment>();
            foreach (DataRow row in dataTable.Rows)
            {


                list.Add(new Appointment(
                    int.Parse(row["id"].ToString()),
                    int.Parse(row["patient_id"].ToString()),
                    int.Parse(row["illness_id"].ToString()),
                    int.Parse(row["department_id"].ToString()),
                    int.Parse(row["doctor_id"].ToString()),
                   int.Parse(row["clinic_time_id"].ToString()),
                    row["created_by"].ToString(),
                   (DateTime)(row["created_time"]),
                    row["updated_by"].ToString(),
                   (DateTime)(row["updated_time"]),
                    int.Parse(row["state"].ToString())

                    )) ;
            }
            return list;
        }

    }
}
